<?php

namespace app\shop\controller\plus;

use app\shop\controller\Controller;
use app\shop\model\plus\lottery\Lottery as LotteryModel;
use app\shop\model\plus\lottery\Record as RecordModel;
use app\shop\model\plus\lottery\LotteryPrize as LotteryPrizeModel;
use app\shop\model\settings\Express as ExpressModel;

/**
 * 转盘控制器
 */
class Lottery extends Controller
{
    /**
     *修改
     */
    public function setting()
    {

        $model = new LotteryModel();
        if ($this->request->isGet()) {
            $data = $model->getLottery();
            $data['prize'] = $data ? LotteryPrizeModel::detail($data['lottery_id']) : [];
            return $this->renderSuccess('', compact('data'));
        }
        if ($model->edit($this->postData())) {
            return $this->renderSuccess('修改成功');
        }
        return $this->renderError($model->getError() ?: '修改失败');
    }

    /*
     * 转盘记录列表
     */
    public function record()
    {
        $model = new RecordModel();
        $list = $model->getList($this->postData());
        // 物流公司列表
        $model = new ExpressModel();
        $expressList = $model->getAll();
        return $this->renderSuccess('', compact('list', 'expressList'));
    }

    /**
     * 获取下架奖项
     * @param null $id
     */
    public function award()
    {
        $model = new LotteryModel();
        $data = $model->getLottery();
        $list = LotteryPrizeModel::getlist($this->postData(), $data);
        return $this->renderSuccess('', compact('list'));
    }

    /*
     * 发货
     */
    public function send($record_id)
    {
        $model = RecordModel::detail($record_id);
        if ($model->send($this->postData())) {
            return $this->renderSuccess('发货成功');
        }
        return $this->renderError($model->getError() ?: '发货失败');
    }

    /**
     * 抽奖记录导出
     */
    public function export()
    {
        $model = new RecordModel();
        return $model->exportList($this->postData());
    }
}